﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EquipSecs300
{
    public static class UtilitiesType
    {
        public static bool IsCustomClass(this Type type)
        {
            bool isCustomClass = !type.IsPrimitive && !type.IsArray &&
                           type != typeof(string) && type != typeof(void) &&
                           !type.IsConstructedGenericType && !type.IsPointer &&
                           type != typeof(DateTime) && !type.Namespace.StartsWith("System");
            return isCustomClass;
        }

        public static bool IsDictionaryType(this Type type)
        {
            return typeof(IDictionary).IsAssignableFrom(type);
        }

        public static bool IsDictionaryType_2(this Type type)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            return typeof(IDictionary).IsAssignableFrom(type) &&
                   type.IsGenericType &&
                   type.GetGenericArguments().Length == 2;
        }
        /// <summary>
        /// Arry&List
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [Obsolete]
        public static bool IsListType(this Type type)
        {
            return typeof(IList).IsAssignableFrom(type);
        }
        /// <summary>
        /// Arry&List
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [Obsolete]
        public static bool IsListType_2(this Type type)
        {
            if (type == null)
                return false;

            return type.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IList<>))
                || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>));
        }
        /// <summary>
        /// List
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsListNotArray(this Type type)
        {
            return type.IsGenericType &&
                   type.GetGenericTypeDefinition() == typeof(List<>) &&
                   !type.GetInterfaces().Contains(typeof(Array));
        }

        public static bool IsArrayType(this Type type)
        {
            return type.IsArray && !type.IsGenericType;
        }

       
    }
}
